﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace MebAppMarketplace.Common.Helpers
{
    public class TextHelper
    {
        public static bool IsValidEmail(string email)
        {
            if (string.IsNullOrEmpty(email))
                return false;
            email = email.ToLower();
            var pair = email.Split('@');
            if (pair.Length != 2)
                return false;
            if (!Regex.IsMatch(pair[0], "^[a-z0-9]"))
            {
                return false;
            }
            if (!Regex.IsMatch(pair[0], "[a-z0-9]$"))
            {
                return false;
            }
            if (!Regex.IsMatch(pair[0], "[a-z0-9\\.]+"))
            {
                return false;
            }
            var pair2 = pair[1].Split('.');
            if (pair2.Length < 2)
                return false;
            if (pair2.Any(x => !Regex.IsMatch(x, "^[a-z0-9]+$")))
                return false;
            if (!Regex.IsMatch(pair2.Last(), "^[a-z]+$")) //com,org, ...
                return false;
            return true;
        }
    }
}
